<template>
  <!-- 查询 -->
  <div class="app-container">

    <el-table v-loading="loading" :data="boardGateList" >
      <el-table-column class-name="small-padding fixed-width" label="值机口编号" align="center" prop="boardGateNo" fixed="left">
        <template slot-scope="scope">
          <span>{{ getBoardGateName(scope.row.boardGateNo) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="始发地-目的地" align="center" prop="origin" >
        <template slot-scope="scope">
          <span>{{ scope.row.origin + '-' + scope.row.destination}}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="small-padding fixed-width" label="航班号" align="center" prop="flightNo" >
      </el-table-column>
      <el-table-column label="是否中转" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.row.isTransfer }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right">
        <template slot-scope="scope">
          <el-button
            :disabled="ckFlag"
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handleUpdate(scope.row)"
          >{{scope.row.zdCkXlRkCount >= 1 ? '出库' : '分配早到航班' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改航班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form v-if="this.zdXlRkCount < 1" ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="早到航班号" prop="flightNo">
          <el-select v-model="form.flightNo" clearable style="width: 228px">
            <el-option
              v-for="item in form.flightNoList"
              :key="item.key"
              :label="item.key"
              :value="item.key"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div v-else>{{'航班[' + this.flightNo + ']已满足出库条件'}}</div>
      <div slot="footer" class="dialog-footer">
        <el-button :disabled="ckFlag" type="primary" @click="submitForm">出 库</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBoardGate, getBoardGate, updateBoardGate } from "@/api/ground/boardGate";

export default {
  name: "BoardGate",
  dicts: ['board_gate_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 登机口表格数据
      boardGateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boardGateNo:'',
        flightNo: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flightNo: [
          { required: true, message: "航班不能为空", trigger: "change" }
        ],
      },
      zdXlRkCount:'',
      flightNo:'',
      boardGateId:'',
      ckFlag: false
    };
  },
  created() {
    this.getList();
  },
  computed:{
  },
  methods: {

    /** 查询登机口列表 */
    getList() {
      this.loading = true;
      listBoardGate(this.queryParams).then(response => {
        this.boardGateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form= {};
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryIsTransfer = '';
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.zdXlRkCount = row.zdCkXlRkCount;
      if(row.zdCkXlRkCount < 1){
        this.reset();
        const boardGateId = row.boardGateId;
        getBoardGate(boardGateId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "早到航班信息";
        });
      }else {
        this.flightNo = row.flightNo;
        this.boardGateId = row.boardGateId;
        this.open = true;
        this.title = "早到航班信息";
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.ckFlag = true;
      if(this.zdXlRkCount >= 1 ){
        this.$set(this.form, "zdCkXlRkCount", this.zdXlRkCount);
        this.$set(this.form, "flightNo", this.flightNo);
        this.$set(this.form, "boardGateId", this.boardGateId);
        updateBoardGate(this.form).then(response => {
          this.$modal.msgSuccess("出库成功");
          this.open = false;
          this.ckFlag = false;
          this.getList();
        });
      }else {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$set(this.form, "zdCkXlRkCount", this.zdXlRkCount);
            updateBoardGate(this.form).then(response => {
              this.$modal.msgSuccess("出库成功");
              this.open = false;
              this.ckFlag = false;
              this.getList();
            });
          }
        });
      }
    },

    //获取仓库编号
    getBoardGateName(val){
      if(val == 1){
        return "A口";
      }else if(val == 2){
        return "B口";
      }
    },

  }
};
</script>

<style lang="scss" scoped>

</style>
